import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

file_path = r"D:\python charm\01\可视化\销售看板参考\成本明细_dh12.xlsx"
dh12 = pd.read_excel(file_path)

# 总成本
total_cost = dh12['成本'].sum()

# 准备数据
categories = dh12['类别']
costs = dh12['成本']
percentages = (costs / total_cost) * 100

# 设置图表风格
plt.figure(figsize=(10, 10), facecolor='#2E4057')

# 子图参数
colors = ['#4F81BD', '#C0504D', '#9BBB59', '#8064A2']
rows, cols = 2, 2  # 2x2 布局

# 绘制每个类别的圆环图
for i, (category, cost, percentage) in enumerate(zip(categories, costs, percentages)):
    ax = plt.subplot(rows, cols, i + 1)
    ax.set_facecolor('#2E4057')

    # 绘制圆环图
    wedges, texts = ax.pie(
        [percentage, 100 - percentage],
        radius=1,
        colors=[colors[i], '#D9D9D9'],  # 当前类别颜色和灰色背景
        startangle=90,  # 设置起始角度为 -45 度
        wedgeprops={'width': 0.3, 'edgecolor': 'white'}
    )

    # 中心文本
    ax.text(0, 0.1, f"{percentage:.2f}%", ha='center', va='center', fontsize=24, color='white')
    ax.text(0, -0.3, f"{category}\n用合计", ha='center', va='center', fontsize=20, color='white')
    ax.text(0, -0.6, f"{cost:.2f}", ha='center', va='center', fontsize=20, color='white')

    # 设置标题
    ax.set_title(category, fontsize=20, color='white', pad=10)

# 整体标题
plt.suptitle("成本费用", fontsize=20, color='white', y=0.95)

# 调整布局
plt.tight_layout(rect=[0, 0, 1, 0.93])

plt.savefig(r'D:\python charm\01\可视化\销售看板参考\图片\成本.png')

plt.show()